package com.tdam2011.grupo05.control.adapters;

import java.text.SimpleDateFormat;
import java.util.Locale;

import com.tdam2011.grupo05.R;
import com.tdam2011.grupo05.database.manager.DataManager;
import com.tdam2011.grupo05.entities.message.MessageWeb;
import com.tdam2011.grupo05.entities.message.MessageWeb.State;
import com.tdam2011.grupo05.entities.message.MessageWeb.Type;
import com.tdam2011.grupo05.entities.thread.ThreadWeb;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class MessageCursorAdapter extends SimpleCursorAdapter {

	static final String TAG = "MessageCursorAdapter";
	private final DataManager manager;
	private final LayoutInflater mInflater;
	private final SimpleDateFormat formatter = new SimpleDateFormat(
			"dd/MM/yyyy HH:mm", Locale.getDefault());

	public MessageCursorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to, int flags) {
		super(context, layout, c, from, to, flags);
		manager = DataManager.getInstance(context);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	protected void onContentChanged() {
		super.onContentChanged();
	}

	@Override
	public View getView(int position, View contentView, ViewGroup root) {

		View view = null;
		MessageWeb m = manager.getMessageFromCursor((Cursor) getItem(position));
		ThreadWeb t = manager.getThread(m.getIdThread());
		if (contentView == null) {
			if (m != null) {

				if (m.getType() == Type.INCOMING) {
					view = mInflater.inflate(R.layout.message_item_incoming,
							root, false);
					((TextView) view
							.findViewById(R.id.message_web_contact_name))
							.setText(t.getName());
				} else {
					view = mInflater.inflate(R.layout.message_item_outgoing,
							root, false);
					((TextView) view
							.findViewById(R.id.message_web_contact_name))
							.setText("Yo");

				}
				view.setTag(m);
			}
		} else {
			view = contentView;
			if (m.getType() == Type.INCOMING
					&& ((MessageWeb) view.getTag()).getType() == Type.OUTGOING) {
				view = mInflater.inflate(R.layout.message_item_incoming, root,
						false);
				((TextView) view.findViewById(R.id.message_web_contact_name))
						.setText(t.getName());
			} else if (m.getType() == Type.OUTGOING
					&& ((MessageWeb) view.getTag()).getType() == Type.INCOMING) {
				view = mInflater.inflate(R.layout.message_item_outgoing, root,
						false);
				((TextView) view.findViewById(R.id.message_web_contact_name))
						.setText("Yo");

			}

		}
		((TextView) view.findViewById(R.id.txt_web_message)).setText(m
				.getText());
		((TextView) view.findViewById(R.id.txt_timestamp)).setText(formatter
				.format(m.getTimestamp()));
		view.setTag(m);
		if (m.getType() == Type.OUTGOING) {
			if (m.getState() == State.SENDED) {
				((ImageView) view.findViewById(R.id.img_message_web_check_ok))
						.setVisibility(View.VISIBLE);
				((ImageView) view
						.findViewById(R.id.img_message_web_check_error))
						.setVisibility(View.INVISIBLE);
			} else if (m.getState() == State.ERROR) {
				((ImageView) view.findViewById(R.id.img_message_web_check_ok))
						.setVisibility(View.INVISIBLE);
				((ImageView) view
						.findViewById(R.id.img_message_web_check_error))
						.setVisibility(View.VISIBLE);
			} else {
				((ImageView) view.findViewById(R.id.img_message_web_check_ok))
						.setVisibility(View.INVISIBLE);
				((ImageView) view
						.findViewById(R.id.img_message_web_check_error))
						.setVisibility(View.INVISIBLE);
			}
		} else {
			if (m.getState() == State.READED) {
				((ImageView) view.findViewById(R.id.img_message_web_check_ok))
						.setVisibility(View.VISIBLE);

			} else if (m.getState() == State.UNREAD) {
				((ImageView) view.findViewById(R.id.img_message_web_check_ok))
						.setVisibility(View.INVISIBLE);
				m.setState(State.READED);
				manager.updateMessage(m);
			} else {
				((ImageView) view.findViewById(R.id.img_message_web_check_ok))
						.setVisibility(View.INVISIBLE);
			}
		}

		return view;
	}

}
